#include "../include/evt_thread.h"
#include "../include/evt_dispather.h"
#include "../include/thread_warper.h"


evt_thread::evt_thread()
:m_dispather_ptr(NULL),
m_mutex(),
m_cond_mutex(),
m_cond(),
m_thread(new thread_warper(boost::bind(&evt_thread::thread_func,this) ))
{
    //ctor
}

evt_thread::~evt_thread()
{
    if(m_thread->is_running() ) m_thread->join();
}

evt_dispather*  evt_thread::start()
{
    m_thread->start();
    {
        boost::mutex::scoped_lock   lock(m_mutex);
        while(m_dispather_ptr == NULL){
            boost::mutex::scoped_lock slock(m_cond_mutex);
            m_cond.wait(m_cond_mutex);
        }
    }
    return m_dispather_ptr;

}
void        evt_thread::join()
{
    m_thread->join();
}
void            evt_thread::thread_func()
{
    evt_dispather disp;
    {
        boost::mutex::scoped_lock   lock(m_mutex);
        m_dispather_ptr = &disp;
        m_cond.notify_one();
    }
    m_dispather_ptr->main_loop();
}
